package day9;
//通过Synchronized 模拟多线程 轮流并发执行
//假设是三个线程A，B，C 要求顺序是ABCABC...
//1.可以使用逻辑判断控制流程 无需使用wait和notify()...
//缺点：浪费很多次循环次数，而且代码特别冗余
//2.借助于它wait()和notify()实现
public class Demo5 {
    int n =1;//1(A) 2(B) 3(C)
    public synchronized void printA(){
        if (n ==1){
            System.out.println(Thread.currentThread().getName());
            n=2;
        }
    }
    public synchronized void printB(){
        if (n==2){
            System.out.println(Thread.currentThread().getName());
            n =3;
        }
    }
    public synchronized void printC(){
        if (n==3){
            System.out.println(Thread.currentThread().getName());
            n =1;
        }
    }


    public static void main(String[] args) {
        Demo5 d = new Demo5();

        new Thread(()->{
            while (true) {
                d.printA();
            }
        },"A").start();
        new Thread(()->{
            while (true) {
                d.printB();
            }
        },"B").start();
        new Thread(()->{
            while (true) {
                d.printC();
            }
        },"C").start();
    }
}
